{template 'common/header'}
<script>
    document.onkeydown=function(e){
        var a=e||window.event;//加这个火狐下不会报 event is  undefind
        if (a.keyCode == 13){
            var key = $("#number_key").val();
            var f_key = $("#freight_key").val();
            if(key >= 0){
                key++;
                $("#number"+key).focus();
            }
            if (f_key >= 0) {
                f_key++;
                $("#freight"+f_key).focus();
            }
        }
    }
    window.onbeforeunload=function(e){
        var save_data = $("#save-data").val();
        if(save_data == 1){
            var url = "{php echo $this->createWebUrl('express',array('op' => 'drop_unset_goods'))}";
            var order_id = "{$order_id}";
            $.ajax({
                type : 'post',
                url : url,
                data : {'order_id' : order_id},
                success : function(data){

                }
            });
//            var e=window.event||e;
//            e.returnValue=("是否确定退出？");
        }
    }
    $(function(){
        var difference_server = "{$difference_server}";
        if(difference_server == true){
            alert("订单中存在服务社与订单服务社不一致的内容！");
        }
        $(".express-no").first().focus();
        //增加一条快递信息记录
        $(".add_one").on('click',function(){
            $("#save-data").val(2);
            var order_id = {$order['id']};
            if(order_id > 0 ){
                var url = "{php echo $this->createWebUrl('express',array('op' => 'add_one'))}";
                $.ajax({
                    type : 'post',
                    url : url,
                    data : {'order_id' : order_id},
                    success : function(data){
                        if(data == 111){
                            location.reload();
                        }else{
                            alert(data);
                        }
                    }
                });
            }
        });
        //修改快递公司，同时修改单价
        $(".choose-company").on('change',function(){
            var company_id = $(this).val();
            var goods_id = $(this).attr('goods_id');
            if(company_id > 0){
                var url = "{php echo $this->createWebUrl('express',array('op' => 'get_company'))}";
                $.ajax({
                    type : 'post',
                    url : url,
                    data : {'company_id' : company_id},
                    success : function(data){
                        var json = JSON.parse(data);
                        if(json['code'] == 0){
                            $("#freight"+goods_id).val(json['freight']);
                            if(json['had_freight'] > 0) {
                                $("#had_freight" + goods_id).val(json['had_freight']); //填写默认已收款内容
                            }
                        }

                    }
                });
            }
        });
        $(".goods-numbers").focus(function(){
            var key = $(this).attr("key");
            $("#number_key").val(key);
            $("#freight_key").val(-2);
        });
        $(".freight").focus(function(){
            var key = $(this).attr("key");
            $("#freight_key").val(key);
            $("#number_key").val(-2);
        });
        $(".goods-numbers").blur(function(){
            get_price();
        });
        $(".freight").blur(function(){
            get_price();
        });
        $(".gen-pay").blur(function(){
            get_price();
        });
        $(".get-all-money").on('click',function(){
//            if(confirm("是否确定已经全部收款？")) {
//                var order_id = "{$order['id']}";
//                if (order_id > 0) {
//                    var url = "{php echo $this->createWebUrl('orders',array('op' => 'get_all_money'))}";
//                    $.ajax({
//                        type: 'post',
//                        url: url,
//                        data: {'order_id': order_id},
//                        success: function (data) {
//                            location.reload();
//                        }
//                    });
//                }
//            }
            var goods_ids = {$goods_ids};
            for(var i = 0; i < goods_ids.length; i++){
                var numbers = parseInt($(".number"+goods_ids[i]).val());
                var freight = parseInt($(".fre"+goods_ids[i]).val());
                var total = numbers*freight;
                if(numbers > 0 && freight > 0) {
                    $("#had_freight" + goods_ids[i]).val(total);
                }
            }
        });
        $(".member-key").keyup(function(){
            var key_word = $(this).val();
            var url = "{php echo $this->createWebUrl('orders',array('op' => 'find_member_by_key'))}";
            $.ajax({
                type : "post",
                url : url,
                data : {'key_word' : key_word},
                success : function(data){
                    $("#member-list").html(data);
                }
            });
        });
        $(".get-name").click(function(){
            var order_goods_id = $(this).parent().parent().attr('order_goods_id');
            $("#change-goods-id").val(order_goods_id);
            $("#change-type").val('get-name');
        });
        $(".del-goods").click(function(){
            if(confirm("是否确定删除该记录？")){
                var order_goods_id = $(this).parent().parent().attr('order_goods_id');
                if(order_goods_id){
                    var url = "{php echo $this->createWebUrl('express',array('op' => 'del_goods'))}";
                    $.ajax({
                        type : "post",
                        url : url,
                        data : {'id' : order_goods_id},
                        success : function(data){
                            if(data == 111){
                                location.reload();
                            }else{
                                alert(data);
                            }
                        }
                    });
                }
            }
        });
        $(".cancel").on('click',function(){
            var save_data = $("#save-data").val();
            if(save_data == 1){
                var url = "{php echo $this->createWebUrl('express',array('op' => 'drop_unset_goods'))}";
                var order_id = "{$order_id}";
                $.ajax({
                    type : 'post',
                    url : url,
                    data : {'order_id' : order_id},
                    success : function(data){
                        location.href="{php echo $this->createWebUrl('express')}";
                    }
                });
//            var e=window.event||e;
//            e.returnValue=("是否确定退出？");
            }
        });
        $(".get-money").click(function(){
            if(confirm("是否确定收齐本条款项？")){
                var order_goods_id = $(this).parent().parent().attr('order_goods_id');
                if(order_goods_id){
                    var url = "{php echo $this->createWebUrl('cashier',array('op' => 'get_money'))}";
                    $.ajax({
                        type : 'post',
                        url : url,
                        data : {'type' : 'all','goods_id' : order_goods_id},
                        success : function(data){
                            if(data == 111){
                                alert("收款成功！");
                                location.reload();
                            }else{
                                alert(data);
                            }
                        }
                    });
                }
            }
        });
        //修改服务社，要重新load一次所有的编码
        $(".change-server").on('change',function(){
            $("#save-data").val(2);
           var server_id = $(this).val();
           var url = "{php echo $this->createWebUrl('express',array('op' => 'change_server'))}";
           var order_id = {$order['id']};
           $.ajax({
               type : 'post',
               url : url,
               data : {'server_id' : server_id, 'order_id' : order_id},
               success : function (data) {
                   location.reload();
               }
           });
        });
        $(".remark").blur(function(){
            var remark = $(this).val();
            var order_id = "{$order['id']}";
            var url = "{php echo $this->createWebUrl('orders',array('op' => 'change_order'))}";
            $.ajax({
                type : "post",
                url : url,
                data : {'remark' : remark , 'order_id' : order_id},
                success : function(data){
                    if(data == 111){
//                        location.reload();
                    }else{
                        alert(data);
                    }
                }
            });
        });
        $('#finish').on('click', function () {
            $("#save-data").val(2);
            var can = true;
            $('input.goods-numbers').each(function(){
                if($(this).val() == 0){
                    can = false;
                    $(this).val('');
                    $(this).focus();
                }
            });
            if(can){
                $("#form").submit();
            }else{
                alert("还有商品数量未填写！");
                return false;
            }
        });
    });
    //修改数量以及单价的时候，自动计算运费，代付款
    function get_price(){
        var goods_count = '{$goods_count}';
        var goods_count = parseInt(goods_count);
        var total_number = 0;
        var total_freight = 0;
        var total_gen = 0;
        for(var i = 0; i < goods_count; i++){
            var number = $("#number"+i).val();
            var freight = $("#freight"+i).val();
            var gen = $("#gen_pay"+i).val();
            number = parseInt(number);
            freight = parseInt(freight);
            gen = parseInt(gen);
            if(number >= 0) {
                total_number += number;
                if(freight >= 0){
                    total_freight += freight*number;
                }
            }
            if(gen >= 0) {
                total_gen += gen;
            }
        }
        $("#total_numbers").val(total_number);
        $("#total_freight").val(total_freight);
        $("#total_gen").val(total_gen);
    }
    function change_from_member(member_name) {
        var goods_id = $("#change-goods-id").val();
        var change_type = $("#change-type").val();
            $("#order-member").val(member_name);
        $("#close").click();
    }
</script>
<ul class="nav nav-tabs">
    <li ><a href="{php echo $this->createWebUrl('express', array('op'=>'order_list'))}">快递订单</a></li>
    <li class="active"><a href="{php echo $this->createWebUrl('express', array('op'=>'add'))}">添加订单</a></li>
    <li ><a href="{php echo $this->createWebUrl('express', array('op'=>'company'))}">快递公司管理</a></li>
    <li ><a href="{php echo $this->createWebUrl('express', array('op'=>'server'))}">服务社管理</a></li>
</ul>
<div class="main">
    <form method="post" action="{php echo $this->createWebUrl('express',array('op' => 'goods_post'))}" id="form" >
        <div class="panel panel-primary">
            <div class="panel-body">
                <div class="row">
                    <div class="col-xs-3">
                        <label class="col-xs-12 col-sm-4 col-md-4 control-label">客户</label>
                        <div class="col-sm-8 col-xs-12">
                            <input type="text" id="order-member" name="order_member" class="form-control order-member" value="{$member['name']}" readonly="readonly" {if $order['status'] <= 1}data-toggle="modal" data-target="#myModal"{/if} />
                        </div>
                    </div>
                    <div class="col-xs-3">
                        <label class="col-xs-12 col-sm-4 col-md-4 control-label">订单日期</label>
                        <div class="col-sm-8 col-xs-12">
                            <input type="date" class="form-control" name="order_day" value="{$order['day']}" {if $order['status'] == 2} readonly="readonly" {/if} />
                        </div>
                    </div>
                    <div class="col-xs-3">
                        <label class="col-xs-12 col-sm-4 col-md-4 control-label">单号</label>
                        <div class="col-sm-8 col-xs-12">
                            <input type="text" class="form-control" value="{$order['order_no']}" readonly="readonly" />
                        </div>
                    </div>
                    <div class="col-xs-3">
                        <label class="col-xs-12 col-sm-4 col-md-4 control-label">服务社</label>
                        <div class="col-sm-8 col-xs-12">
                            <select class="form-control change-server" name="server_id">
                                {loop $servers $server}
                                <option value="{$server['id']}" {if $server['id'] == $order['order_region']} selected="selected" {/if}>{$server['name']}</option>
                                {/loop}
                            </select>
                        </div>
                    </div>
                </div>
                <br />
                <div class="row">
                    <div class="col-xs-3">
                        <label class="col-xs-12 col-sm-4 col-md-4 control-label">电话</label>
                        <div class="col-sm-8 col-xs-12">
                            <input type="text" class="form-control" value="{$member['phone']}" readonly="readonly" />
                        </div>
                    </div>
                    <div class="col-xs-3">
                        <label class="col-xs-12 col-sm-4 col-md-4 control-label">总件数</label>
                        <div class="col-sm-8 col-xs-12">
                            <input type="text" class="form-control" value="{$goods_total}" id="total_numbers" readonly="readonly" />
                        </div>
                    </div>
                    <div class="col-xs-3">
                        <label class="col-xs-12 col-sm-4 col-md-4 control-label">总运费</label>
                        <div class="col-sm-8 col-xs-12">
                            <input type="text" class="form-control" value="{$freight_total}" id="total_freight" readonly="readonly" />
                        </div>
                    </div>
                    <div class="col-xs-3">
                        <label class="col-xs-12 col-sm-4 col-md-4 control-label">代付款</label>
                        <div class="col-sm-8 col-xs-12">
                            <input type="text" class="form-control" value="{$gen_pay_total}" id="total_gen" readonly="readonly" />
                        </div>
                    </div>

                </div>
                <br />
                <div class="row">
                    <div class="col-xs-3">
                        <label class="col-xs-12 col-sm-4 col-md-4 control-label">已收运费</label>
                        <div class="col-sm-8 col-xs-12">
                            <input type="text" class="form-control" value="{$had_freight_total}" readonly="readonly" />
                        </div>
                    </div>
                </div>
                <br />
                <div class="row">
                    <div class="col-xs-12">
                        <label class="col-xs-12 col-sm-3 col-md-1 control-label">备注</label>
                        <div class="col-sm-9 col-xs-12">
                            <input type="text" class="form-control" name="order_remark" value="{$order['remark']}"/>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <button type="button" class="btn btn-default add_one">添加快件</button>
        <input type="hidden" value="" id="number_key" />
        <input type="hidden" value="" id="freight_key" />
        <input type="hidden" name="order_id" value="{$order['id']}"  />
        <table class="table">
            <tr>
                <th>商品</th>
                <!--<th>单位</th>-->
                <th>数量</th>
                <th>运费单价</th>
                <th>已收运费</th>
                <th>代付款</th>
                <!--<th>手续费</th>-->
                <!--<th>服务社</th>-->
                <!--<th>收款员</th>-->
                <!--<th>已收运费</th>-->
                <th>快递公司</th>
                <!--<th>收货人</th>-->
                <th>快递单号</th>
                <th>编码</th>

                <th>备注</th>
                <th>操作</th>
            </tr>
            {loop $order_goods $key $goods}
            <tr  order_goods_id="{$goods['id']}">
                <td>{$goods['goods_name']}</td>
                <td>
                    <input type="text" value="{if $goods['numbers'] > 0}{$goods['numbers']}{/if}" key="{$key}" id="number{$key}" class="form-control goods-numbers number{$goods['id']}" name="numbers{$goods['id']}" {if $goods['status'] == 2 || $order['status'] == 2} readonly="readonly" {/if}  onfocus="this.select()"/>
                </td>
                <td>
                    <input type="text" value="{$goods['freight']}" class="form-control freight fre{$goods['id']}" id="freight{$goods['id']}" name="freight{$goods['id']}"  onfocus="this.select()" {if $goods['status'] == 2 || $order['status'] == 2} readonly="readonly" {/if}  onfocus="this.select()" />
                </td>
                <td>
                    <input type="text" class="form-control" value="{$goods['had_freight']}" id="had_freight{$goods['id']}" name="had_freight{$goods['id']}" {if $goods['status'] == 2 || $order['status'] == 2} readonly="readonly" {/if}  />
                </td>
                <td>
                    <input type="text" value="{if $goods['gen_pay'] > 0}{$goods['gen_pay']}{/if}" id="gen_pay{$key}" name="gen_pay{$goods['id']}" class="form-control gen-pay" id="gen_pay{$key}" {if $goods['status'] == 2 || $order['status'] == 2} readonly="readonly" {/if} />
                </td>
                <!--<td>-->
                    <!--<select class="form-control goods-region" name="server{$goods['id']}" disabled='disabled' >-->
                    <!--{loop $servers $server}-->
                    <!--<option value="{$server['id']}" {if $server['id'] == $goods['region_id']} selected="selected" {/if}>{$server['name']}</option>-->
                    <!--{/loop}-->
                    <!--</select>-->
                <!--</td>-->
                <td>
                    <select class="form-control choose-company" goods_id="{$goods['id']}" name="company{$goods['id']}" {if $goods['status'] == 2 || $order['status'] == 2} disabled='disabled' {/if}>
                        <option>快递公司</option>
                        {loop $companys $company}
                        <option value="{$company['id']}" {if $company['id'] == $goods['company_id']} selected="selected" {/if}>{$company['name']}</option>
                        {/loop}
                    </select>
                </td>
                <td>
                    <input type="text" value="{$goods['express_no']}" class="form-control express-no" name="express_no{$goods['id']}" />
                </td>
                <td>
                    <input type="text" value="{$goods['express_code']}" class="form-control" readonly="readonly" />
                </td>

                <td>
                    <input type="text" class="form-control goods-remark" value="{$goods['remark']}" name="remark{$goods['id']}" {if $goods['status'] == 2} readonly="readonly" {/if} />
                </td>
                <td>
                    {if $goods['status'] <= 1}
                    <!--<button type="button" class="btn btn-primary get-money">收齐</button>-->
                    <button type="button" class="btn btn-danger del-goods">删除</button>
                    {/if}
                </td>
            </tr>
            {/loop}

        </table>
        <div>
            <input type="hidden" value="1" id="save-data" />
            <button type="button" class="btn btn-primary" id="finish">保存</button>
            <button type="button" class="btn btn-danger cancel">取消</button>
            {if $order['status'] <= 1 && $goods['had_freight'] == 0}
            <button type="button" class="btn btn-success get-all-money" style="float: right;">全部收齐</button>
            {/if}
        </div>
    </form>
</div>

<!-- 修改寄货人 -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" id="close" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">双击选择</h4>
            </div>
            <div class="modal-body">
                <div style="width:30%" align="center">
                    <input type="text" class="form-control member-key" placeholder="客户名称/助记码"  />
                </div>
                <br />
                <table class="table" id="member-list">
                </table>
                <input type="hidden" id="change-goods-id" value="0" />
                <input type="hidden" id="change-type" value="" />
            </div>
        </div>
    </div>
</div>
{template 'common/footer'}